//
//  TupianCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/3.
//  Copyright © 2016年 game. All rights reserved.
//

#import "TupianCell.h"
#import "SDPhotoGroup.h"
#import "SDPhotoBrowser.h"
#import "UIButton+WebCache.h"
#import "IslanDescribImageModel.h"

@interface TupianCell ()<SDPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lin1Hight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lin2Hight;

@property (nonatomic,strong) SDPhotoBrowser *browser;

@property(nonatomic,strong) UIView *bacView;


@end

@implementation TupianCell


-(UIView *)bacView{
    if (!_bacView) {
        _bacView = [UIView new];
        _bacView.frame = CGRectMake(0, 0, screen_width, 0);
        [self.contentView addSubview:_bacView];
    }

    return _bacView;
}
- (void)awakeFromNib {
    self.lin1Hight.constant = 0.5;
    self.lin2Hight.constant = 0.5;
    self.selectionStyle  = UITableViewCellSelectionStyleNone;
}


-(void)setImageAry:(NSArray *)imageAry{
    _imageAry = imageAry;
    
        CGFloat xd = 8;
    
    if (_imageAry.count==0) {
        self.cellhight = -1;
        [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
    }else{
        NSInteger count = _imageAry.count;
        
        if (_imageAry.count==1) {
            count=0;
        }
        
        if (_imageAry.count>6) {
            
            count = 6;
        }
        
        for (int i=0; i<count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            btn.backgroundColor = linColor;
            CGFloat ww = (screen_width-xd*4)/3;
            
           btn.imageView.contentMode  = UIViewContentModeScaleAspectFill;
            
            CGFloat row = i%3;
            CGFloat lod = i/3;
            
            CGFloat xx = xd + (xd+ww)*row;
            
            CGFloat yy = xd + (xd+ww)*lod;
            
            btn.frame = CGRectMake(xx, yy, ww, ww);
            
            btn.tag = i+100;
            
            [self.bacView addSubview:btn];
            
            if (i<5) {
            
                IslanDescribImageModel *model = _imageAry[i];
        [btn sd_setImageWithURL:[NSURL URLWithString:model.small_url] forState:UIControlStateNormal];
                
                [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
            }else{
                [btn setTitle:@"查看更多图片" forState:UIControlStateNormal];
                btn.titleLabel.font = font(screen_width*0.04);
                [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                
                [btn addTarget:self action:@selector(More:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            self.cellhight = Height(btn)+10;
            self.bacView.height = self.cellhight+10-1;
        }

        
    }
}

/**
 *  查看更多图片
 */
-(void)More:(UIButton *)btn{
    
    [self.deleget MoreImage:self.imageAry];
    
}


/**
 *  点击图片
 */
-(void)BtnClick:(UIButton *)btn{
    
    _browser = [[SDPhotoBrowser alloc] init];
    _browser.sourceImagesContainerView = self.bacView; // 原图的父控件
    _browser.imageCount = _imageAry.count; // 图片总数
    _browser.delegate = self;
    
    _browser.alpha = 0;
    _browser.currentImageIndex = (int)btn.tag-100;
    
    [_browser show];
    
    [UIView animateWithDuration:0.8 animations:^{
        _browser.alpha = 1;
    }];

}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return nil;
}


- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{

    IslanDescribImageModel *model = self.imageAry[index];
    NSString *urlStr = [model.big_url stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
    return [NSURL URLWithString:urlStr];
}

@end
